import java.util.*;

public class Solution2225 {
    public List<List<Integer>> findWinners(int[][] matches) {
        Set<Integer> set=new HashSet<>();
        Map<Integer,Integer> map=new HashMap<>();
        for (int[] match : matches) {
            set.add(match[0]);
            set.add(match[1]);
            map.put(match[1],map.getOrDefault(match[1],0)+1);
        }
        List<Integer> l0=new ArrayList<>();
        List<Integer> l1=new ArrayList<>();
        for (Integer integer : set) {
            if (map.getOrDefault(integer,0)==0){
                l0.add(integer);
            }else if (map.getOrDefault(integer,0)==1){
                l1.add(integer);
            }
        }
        l0.sort(Comparator.comparingInt(a -> a));
        l1.sort(Comparator.comparingInt(a -> a));
        return Arrays.asList(l0,l1);
    }

    public static void main(String[] args) {
        System.out.println(new Solution2225().findWinners(new int[][]{{1,3},{2,3},{3,6},{5,6},{5,7},{4,5},{4,8},{4,9},{10,4},{10,9}}));
    }
}
